package com.chencs.common.user.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.common.user.service.UserService;
import com.chencs.vo.common.user.UserModel;
import framework.model.ResponseOptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chencs on 2017/5/26.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }

    @RequestMapping("/user/{id}")
    public String findUserById(@PathVariable String id) {
        return userService.findUserById(id);
    }

    @RequestMapping("/userList")
    public String userList() {
        List<UserModel> list = userService.getUsers();
        return JSON.toJSONString(list);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String login(@RequestBody UserModel model) {
        logger.info("login param {}", JSON.toJSON(model));
        String retStr = "";
        if(userService.login(model.getUserCode(),model.getUserPwd())) {
            retStr = ResponseOptUtil.commonSucess();
        } else {
            retStr = ResponseOptUtil.commonMsgError("用户名、密码错误");
        }
        return retStr;
    }
}
